package indi.twj.mybluetool.adapt;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import indi.twj.mybluetool.R;

/**
 * Created by Administrator on 2017/11/6.
 */
public class DeviceListAdapter extends BaseAdapter {
    private List<BluetoothDevice> data;
    private LayoutInflater layoutInflater;
    private Context context;

    public DeviceListAdapter(Context context, List<BluetoothDevice> accountList) {
        this.context = context;
        data = accountList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    //模型
    public final class Modules {
        public TextView name;
        public TextView address;
    }

    @Override
    public int getCount() {
        return  data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Modules modules = null;

        //如果原先没有
        if (view == null) {
            modules = new Modules();
            // get from xml
            view = layoutInflater.inflate(R.layout.device_list_item, null);
            modules.name = view.findViewById(R.id.item_device_name);
            modules.address =  view
                    .findViewById(R.id.item_device_address);
        } else {
            modules = (Modules) view.getTag();
        }
        // 绑定数据
        // modules.image.setBackgroundResource((Integer)data.get(arg0).get("image"));
        modules.name.setText( data.get(i).getName());
        modules.address.setText(data.get(i).getAddress());

        //Log.d("AccountApter", (String) data.get(arg0).get("outcome"));
        return view;

    }
}
